<?php
/**
 * Created by PhpStorm.
 * User: fengyan
 * Date: 17-11-3
 * Time: 下午2:47
 */

namespace App\Http\Controllers\Admin\Portal;


use App\Http\Controllers\Admin\AdminController;
use App\Repositories;
use Illuminate\Http\Request;
class TagController extends AdminController
{
    /**
     * 初始化操作
     * UserController constructor.
     */
    public function __construct()
    {
        parent::__construct();

        $this->setTemplatePrefix(config('admin.systems.template_prefix', 'admin.').'portal.tag.');
    }

    /**
     * 获取当前控制器服务仓库对象
     * @return mixed
     */
    public function getServiceRepository(): Repositories\Portal\TagRepository
    {
        return Repositories\Portal\TagRepository::getInstance();
    }

    /**
     * 列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $data = $this->getServiceRepository()->getAdminPaginateData();

        return $this->fetch('index', ['data'=>$data]);
    }

    /**
     * 更新标签
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function edit(Request $request)
    {
        if (strtolower($request->method()) !== 'post') {
            $id = $request->route('id', 0);

            $detail = $this->getServiceRepository()->getDetailById($id);
            $action = 'add';
            if (!empty($id)) {
                $action = 'edit';
            }

            return $this->fetch('edit', [
                'metaTitle' => '更新标签信息',
                'postUrl'   => get_admin_url('tag/edit'),
                'detail'    => $detail,
                'action'    => $action
            ]);
        }

        $info = $request->input('info', []);

        $action = $request->input('action', 'add');

        $this->getServiceRepository()->save($info, $action);

        return $this->success('标签保存成功');
    }
}